package me.qi.kancha.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import me.qi.kancha.dto.core.PersonDTO;
import me.qi.kancha.mapper.PersonMapper;
import me.qi.kancha.service.PersonService;
import me.qi.kancha.service.base.support.PageData;
import me.qi.kancha.vo.person.PersonInsertVo;
import me.qi.kancha.vo.person.PersonQueryVo;
import me.qi.kancha.vo.person.PersonUpdateVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

/**
* 人物模块-service实现
*
* @author qi
* @date 2022-10-27 03:55:33
*/
@Service
public class PersonServiceImpl extends ServiceImpl<PersonMapper, PersonDTO>
    implements PersonService{

    @Override
    public PageData<PersonDTO> listPaging(Object obj) {
        PersonQueryVo param = (PersonQueryVo) obj;
        LambdaQueryWrapper<PersonDTO> wrapper = new LambdaQueryWrapper<>();

        //动态查询条件
        if (!ObjectUtils.isEmpty(param.getDependCode())) {
            wrapper.eq(PersonDTO::getDependCode, param.getDependCode());
        }
        if (!ObjectUtils.isEmpty(param.getType())) {
            wrapper.eq(PersonDTO::getType, param.getType());
        }

        //动态查询条件 or嵌套  and (a=1 or b=2)
        if (!ObjectUtils.isEmpty(param.getKey())) {
            wrapper.and(
                    queryWrapper -> queryWrapper.like(PersonDTO::getName, param.getKey())
                            .or().like(PersonDTO::getTitle, param.getKey())
            );
        }

        Page page = page(param.pageInstance(), wrapper);//默认排序方式为 created_time desc 前段未传 order 参数时生效

        //更改默认排序方式 前段未传 order 参数时生效
        // Page page = page(param.pageInstance(OrderItem.desc("modified_time")), wrapper);

        return new PageData<>(page);
    }

    @Override
    public PersonDTO insert(Object obj) {
        PersonInsertVo param = (PersonInsertVo) obj;
        PersonDTO dto = new PersonDTO();
        BeanUtils.copyProperties(param, dto);
        save(dto);
        return dto;
    }

    @Override
    public PersonDTO update(Object obj) {
        PersonUpdateVo param = (PersonUpdateVo) obj;
        PersonDTO dto = new PersonDTO();
        BeanUtils.copyProperties(param, dto);
        updateById(dto);
        return getById(dto.getId());
    }
}




